package com.vue3.blog.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.vue3.blog.common.resp.PageVO;
import com.vue3.blog.common.util.DateUtil2;
import com.vue3.blog.domain.dto.ArticleQueryDTO;
import com.vue3.blog.domain.vo.ArchiveArticleDetailVO;
import com.vue3.blog.domain.vo.ArchiveArticleVO;
import com.vue3.blog.domain.vo.ArchiveVO;
import com.vue3.blog.domain.vo.ArticleDetailVO;
import com.vue3.blog.service.IArchiveService;
import com.vue3.blog.service.IArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Mr.小朱同学
 * @version 1.0.0
 * @date 2025/6/17 11:15
 * @description 文章归档
 */
@Service
public class ArchiveService implements IArchiveService {
    @Resource
    private IArticleService articleService;

    /**
     * 文章归档
     */
    @Override
    public List<ArchiveVO> getArchiveList() {
        // 查询所有已经发布的文章
        ArticleQueryDTO dto = new ArticleQueryDTO();
        dto.setIsPage(0);
        dto.setStatusList(CollUtil.toList(1));
        PageVO<ArticleDetailVO> page = articleService.getArticleList(dto);
        if (page == null || page.getRecord() == null || CollUtil.isEmpty(page.getRecord())) {
            return Collections.emptyList();
        }
        List<ArchiveVO> ansList = new ArrayList<>();
        List<ArticleDetailVO> voList = page.getRecord();
        // 统计当年每个月的文章数量
        // 使用 hutool 日期工具类，判断发布时间是否是今年
        List<ArticleDetailVO> archiveList = voList.stream().filter(vo -> DateUtil2.isThisYear(vo.getPublishedTime())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(archiveList)) {
            // 按照年月统计文章
            Map<String, Long> monthNumMap = archiveList.stream().collect(Collectors.groupingBy(vo -> DateUtil2.format(vo.getPublishedTime(), "MM月"), Collectors.counting()));
            monthNumMap.forEach((timeStr, count) -> {
                ArchiveVO vo = new ArchiveVO();
                vo.setTimeStr(timeStr);
                vo.setCount(Math.toIntExact(count));
                ansList.add(vo);
            });
        }
        List<ArticleDetailVO> nonArchiveList = voList.stream().filter(vo -> !DateUtil2.isThisYear(vo.getPublishedTime())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(nonArchiveList)) {
            // 按照发布时间的年份，统计文章数量
            Map<String, Long> yearNumMap = nonArchiveList.stream().collect(Collectors.groupingBy(vo -> DateUtil2.format(vo.getPublishedTime(), "yyyy年"), Collectors.counting()));
            yearNumMap.forEach((timeStr, count) -> {
                ArchiveVO vo = new ArchiveVO();
                vo.setTimeStr(timeStr);
                vo.setCount(Math.toIntExact(count));
                ansList.add(vo);
            });
        }
        return ansList;
    }

    /**
     * 归档文章时间线列表
     */
    @Override
    public List<ArchiveArticleVO> getArchiveArticleList() {
        // 查询所有已经发布的文章
        ArticleQueryDTO dto = new ArticleQueryDTO();
        dto.setIsPage(0);
        dto.setStatusList(CollUtil.toList(1));
        PageVO<ArticleDetailVO> page = articleService.getArticleList(dto);
        if (page == null || page.getRecord() == null || CollUtil.isEmpty(page.getRecord())) {
            return Collections.emptyList();
        }
        List<ArticleDetailVO> voList = page.getRecord();
        List<ArchiveArticleVO> ansList = new ArrayList<>();
        // 按照天分组
        Map<String, List<ArticleDetailVO>> dayMap = voList.stream().collect(Collectors.groupingBy(vo -> DateUtil2.format(vo.getPublishedTime(), "yyyy-MM-dd")));
        dayMap.forEach((time, dataList) -> {
            ArchiveArticleVO result = new ArchiveArticleVO();
            result.setTimestamp(time);
            List<ArchiveArticleDetailVO> detailList = new ArrayList<>();
            // dataList 按照发表时间降序排序
            dataList.sort((o1, o2) -> o2.getPublishedTime().compareTo(o1.getPublishedTime()));
            dataList.forEach(ent -> {
                ArchiveArticleDetailVO detail = new ArchiveArticleDetailVO();
                detail.setPostId(ent.getId());
                detail.setTitle(ent.getTitle());
                detail.setPublishedTime(ent.getPublishedTime());
                detailList.add(detail);
            });
            result.setDetailList(detailList);
            ansList.add(result);
        });
        // 按照发布时间降序排序
        ansList.sort((o1, o2) -> o2.getTimestamp().compareTo(o1.getTimestamp()));
        return ansList;
    }
}